﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Bzzb.Model;
using FreeSql.Internal.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;

namespace Bzzb.Controllers
{
    public class JYCDingQiKongZhiXiangController : BaseController
    {
        private readonly ILogger<JYCDingQiKongZhiXiangController> _logger;
        public JYCDingQiKongZhiXiangController(ILogger<JYCDingQiKongZhiXiangController> logger, IFreeSql fsql) : base(fsql)
        {
            this._logger = logger;
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Index([FromForm] long jyclid, [FromForm] int pageIndex = 1, [FromForm] int pageSize = 10)
        {
            List<JYCDingQiKongZhiXiang> jycdingqikongzhixiangs = await _fsql.Select<JYCDingQiKongZhiXiang>().Where(jycdqkzx => jycdqkzx.Jyclid == jyclid).Count(out var total).Page(pageIndex, pageSize).ToListAsync();
            JObject data = new();
            data.Add("total", total);
            data.Add("list", JArray.FromObject(jycdingqikongzhixiangs));
            return RenderJson(data: data);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Save([FromForm] JYCDingQiKongZhiXiang jycdingqikongzhixiang)
        {
            long id = await _fsql.Insert(jycdingqikongzhixiang).ExecuteIdentityAsync();
            jycdingqikongzhixiang.Id = id;

            return RenderJson(data: jycdingqikongzhixiang);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Delete([FromForm] long id) {
            int affrows = await _fsql.Delete<JYCDingQiKongZhiXiang>().Where(jycdqkzx => jycdqkzx.Id == id).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Update([FromForm] JYCDingQiKongZhiXiang jycdingqikongzhixiang) {
            int affrows = await _fsql.Update<JYCDingQiKongZhiXiang>().SetSourceIgnore(jycdingqikongzhixiang, colval => colval == null).ExecuteAffrowsAsync();
            return RenderJson(data: affrows);
        }

        [HttpPost]
        public async Task<IDictionary<string, Object>> Show([FromForm] long id) {
            JYCDingQiKongZhiXiang jycdingqikongzhixiang = await _fsql.Select<JYCDingQiKongZhiXiang>().Where(jycdqkzx => jycdqkzx.Id == id).ToOneAsync();
            return RenderJson(data: jycdingqikongzhixiang);
        }
    }
}
